from django.shortcuts import render

# Create your views here.

from django.shortcuts import HttpResponse
from .models import Bug


def index(request):
    return HttpResponse("欢迎使用！")


def user_list(request):
    # return HttpResponse("用户列表")
    name = "韩超"
    roles = ["管理员", "CEO", "保安"]
    user_info = {
        "name": "郭智",
        "salary": 100000,
        "role": "CTO"
    }

    return render(
        request,
        "myapp/user_list.html",
        {
            "n1": name,
            "n2": roles,
            "n3": user_info
         }
    )


def user_add(request):
    return HttpResponse("添加用户")


def news(request):
    import requests
    header = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"}
    url = "http://www.chinaunicom.com.cn/api/article/NewsByIndex/2/2023/05/news"
    res = requests.get(url=url, headers=header)
    data_list = res.json()
    print(data_list)

    return render(
        request,
        'myapp/news.html',
        {
            "news_list": data_list
        }
    )

# class PublisherListView(ListView):
#     model = Publisher

from myapp.models import UserInfo
def info_list(request):
    # UserInfo.objects.create(name="张三", password=123123, age=17)
    data_list = UserInfo.objects.all()
    print(data_list)
    return render(request, "myapp/info_list.html", {"data_list": data_list})


def module_data(request, module_name):
    if module_name == "bug":
        data = Bug.objects.all()
    else:
        data = []
    return render(
        request,
        "myapp/module_data.html",
        {'module_name': module_name, "data": data}
    )
